package org.blog.servlets;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.blog.beans.Blog;
import org.blog.beans.Comment;
import org.blog.beans.Entry;
import org.blog.beans.User;
import org.blog.controller.UserController;
import org.blog.exceptions.BlogException;
import org.blog.logic.BlogLogic;
import org.blog.messages.BlogMessage;

/**
 * Servlet implementation class UserSettings
 */
public class EntryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
 
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		action(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		action(request, response);
	}
	
	private void action(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		BlogMessage bm = new BlogMessage();
		
		
		if("add".equals(request.getParameter("do"))) {
			
			getServletContext().getRequestDispatcher("/WEB-INF/jsp/addEntry.jsp").forward(request, response);

		}
		else if("entry".equals(request.getParameter("do"))) {
			int id = Integer.parseInt(request.getParameter("id"));
			Entry entry = BlogLogic.getInstance().getEntry(id);
			request.getSession().setAttribute("entry", entry);
			getServletContext().getRequestDispatcher("/WEB-INF/jsp/entry.jsp").forward(request, response);
		}
		else if("addComment".equals(request.getParameter("do"))) {
			
			int id = Integer.parseInt(request.getParameter("id"));
			Comment comment= new Comment();
			//TODO dodaj date

			try {
				BeanUtils.populate(comment, request.getParameterMap());
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			int userId = -1; //gosc
			if(UserController.isLogged(request.getSession()))
				userId = UserController.getUser(request.getSession()).getId();
			
			
			
			try {
				BlogLogic.getInstance().addComment(id, userId, comment);
			} catch (BlogException e) {

				String error = "Wprowadz prawidlowa wartosc";
				switch(e.getType()) {
				case BlogException.COMMENT :
					bm.setComment(error);
					break;
				}
			}
			
		
			request.setAttribute("message", bm);

			Entry entry = BlogLogic.getInstance().getEntry(id);
			request.getSession().setAttribute("entry", entry);
			getServletContext().getRequestDispatcher("/WEB-INF/jsp/entry.jsp").forward(request, response);
		}
		else{
			
			Blog blog = (Blog)request.getSession().getAttribute("blog");
			
			Entry entry = new Entry();
			//TODO dodaj date
			entry.setDate(null);
			try {
				BeanUtils.populate(entry, request.getParameterMap());
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			try {
				BlogLogic.getInstance().addEntry(blog.getId(), entry);
			} catch (BlogException e) {
				String error = "Wprowadz prawidlowa wartosc";
				switch(e.getType()) {

				case BlogException.TEXT :
					bm.setEntryText(error);
					break;
				case BlogException.TITLE :
					bm.setEntrytitle(error);
					break;
					
					
				}
			}
			request.setAttribute("message", bm);
			blog = BlogLogic.getInstance().getBlog(blog.getId());
			request.getSession().setAttribute("blog", blog);
			
			if(bm.error)
				getServletContext().getRequestDispatcher("/WEB-INF/jsp/addEntry.jsp").forward(request, response);
			else
				getServletContext().getRequestDispatcher("/WEB-INF/jsp/blog.jsp").forward(request, response);
		}
	}

}
